<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="合同编号" prop="contractNo">
        <el-select
          v-model="queryParams.contractNo"
          placeholder="合同编号"
          filterable
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="item in contractList"
            :key="item.id"
            :label="item.contractNo"
            :value="item.contractNo"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账单编号" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入账单编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入客户姓名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户手机号" prop="customerPhone">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入客户手机号"
          clearable
          style="width: 200px"
          maxlength="11"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- 客户类别 -->
      <el-form-item label="客户类别" prop="customerType">
        <el-select
          v-model="queryParams.customerType"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option label="卖方" value="1"></el-option>
          <el-option label="买方" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          v-hasPermi="['system:bill:export']"
          plain
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="客户手机号" align="center" prop="customerPhone" />
      <el-table-column label="房源地址" align="center" prop="propertyAddress" />
      <el-table-column label="账单编号" align="center" prop="billNo" />
      <el-table-column label="客户类别" align="center" prop="customerType">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.customerType == 1"
            >卖方</el-tag
          >
          <el-tag type="danger" v-else>买方</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="缴费项目"
        align="center"
        prop="billType"
        style="width: 200px"
      >
        <template #default="scope">
          <el-tag type="primary"> {{ scope.row.billType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" prop="totalAmount">
        <template #default="scope">
          <span>{{
            scope.row.totalAmount ? `${scope.row.totalAmount}元` : `0元`
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="receivedAmount">
        <template #default="scope">
          <span>{{
            scope.row.receivedAmount ? `${scope.row.receivedAmount}元` : `0元`
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center" prop="status">
        <template #default="scope">
          <el-tag type="danger" v-if="scope.row.status == 0">未付款</el-tag>
          <el-tag type="primary" v-else-if="scope.row.status == 1"
            >部分付款</el-tag
          >
          <el-tag type="success" v-else-if="scope.row.status == 2"
            >已付全款</el-tag
          >
          <el-tag type="info" v-else>退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="updateTime">
        <template #default="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-if="
              scope.row.status != 2 && ![24, 25, 19].includes(scope.row.itemId)
            "
            link
            type="primary"
            @click="handlePay(scope.row)"
            v-hasPermi="['system:bill:operationPayment']"
            >缴费</el-button
          >
          <el-button link type="primary" @click="handlePrint(scope.row)"
            >打印</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 打印 -->
    <el-dialog
      title="打印"
      v-model="printComfirm"
      width="1000px"
      append-to-body
    >
      <PrintDetail ref="printRef" :form="printForm" />
      <template #footer>
        <el-button type="primary" @click="print">打印</el-button>
        <el-button @click="printComfirm = false">取 消</el-button>
      </template>
    </el-dialog>
    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="缴费项目" prop="payCategoryId">
          <el-radio-group v-model="form.payCategoryId">
            <el-radio
              v-for="item in payTypeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
            ></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="付款状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="部分付款" :value="1"></el-radio>
            <el-radio label="已付全款" :value="2"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="付款金额" prop="partPrice" v-if="form.status == 1">
          <el-input v-model="form.partPrice" placeholder="请输入付款金额" />
        </el-form-item>
        <!-- 应付金额 -->
        <el-form-item label="应付金额" prop="totalAmount">
          <span>{{ form.totalAmount }}元</span>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BillList">
import {
  listBill,
  selectAllPayType,
  operationPayment,
} from "@/api/business/billList";
import PrintDetail from "./components/printDetail.vue";
const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const printComfirm = ref(false);
const printForm = ref({});
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    payCategoryId: [
      { required: true, message: "请选择缴费项目", trigger: "change" },
    ],
    status: [{ required: true, message: "请选择付款状态", trigger: "change" }],
    partPrice: [{ required: true, message: "请输入付款金额", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

function handlePrint(row) {
  printForm.value = row;
  printComfirm.value = true;
}

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listBill(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {};
  proxy.resetForm("postRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
const payTypeList = ref([]);
function getPayTypeList() {
  selectAllPayType().then((response) => {
    payTypeList.value = response.data;
  });
}

/** 新增按钮操作 */
function handlePay(row) {
  open.value = true;
  title.value = "缴费";
  getPayTypeList();
  form.value = {
    id: row.id,
    totalAmount: row.totalAmount,
  };
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      operationPayment(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/system/bill/export",
    {
      ...queryParams.value,
    },
    `bill_${new Date().getTime()}.xlsx`
  );
}

getList();
const printRef = ref(null);
function print() {
  console.log(printRef.value?.printArea);
  const printContent = printRef.value?.printArea;
  const printWindow = window.open("", "", "width=1000,height=600");
  printWindow.document.write(`
    <html>
      <head>
        <title>打印</title>
        <style>
          .receipt-container {
            width: 800px;
            margin: 0 auto;
            border: 1px solid #333;
            padding: 10px 20px;
            font-size: 14px;
            font-family: SimSun, serif;
            position: relative;
          }
          .print-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
          }
          .title {
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            margin: 0;
            width: 70%;
          }
          .top-info {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            font-size: 14px;
            margin-bottom: 10px;
          }
          .main-box {
            display: flex;
            margin-bottom: 10px;
            align-items: center;
          }
          .left-tip {
            width: 15%;
            /* padding: 10px; */
            padding-right: 10px;
            font-size: 12px;
          }
          .right-form-box {
            width: 81%;
          }
          .right-form {
            width: 100%;
            padding: 10px;
            border: 1px solid #333;
          }
          .underline {
            border-bottom: 1px solid #000;
            display: inline-block;
            min-width: 150px;
          }
          .blank {
            min-width: 100px;
          }
          .ml-10 {
            margin-left: 10px;
          }
          .print-btn {
            margin-top: 20px;
          }
          .form-row {
            margin-bottom: 20px;
          }
        </style>
      </head>
      <body>
        ${printContent.outerHTML}
      </body>
    </html>
  `);
  printWindow.document.close();
  printWindow.print();
}
</script>
